package com.nbsaas.boot.user.ext.resource;

import com.nbsaas.boot.rest.filter.Filter;
import com.nbsaas.boot.rest.request.RequestId;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.user.api.apis.UserOauthConfigApi;
import com.nbsaas.boot.user.api.apis.UserOauthTokenApi;
import com.nbsaas.boot.user.api.domain.field.UserOauthConfigField;
import com.nbsaas.boot.user.api.domain.field.UserOauthTokenField;
import com.nbsaas.boot.user.api.domain.request.UserOauthTokenRequest;
import com.nbsaas.boot.user.api.domain.response.UserOauthConfigResponse;
import com.nbsaas.boot.user.api.domain.response.UserOauthTokenResponse;
import com.nbsaas.boot.user.api.domain.simple.UserOauthTokenSimple;
import com.nbsaas.boot.user.ext.apis.UserOauthTokenExtApi;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


public class UserOauthTokenExtResource implements UserOauthTokenExtApi {

    @Resource
    private UserOauthTokenApi userOauthTokenApi;

    @Resource
    private UserOauthConfigApi userOauthConfigApi;

    @Transactional
    @Override
    public ResponseObject<?> bind(String config, String openId, String unionId) {
        return bind(config, null, openId, unionId);
    }

    @Transactional
    @Override
    public ResponseObject<?> bind(String config, Long userId, String openId, String unionId) {
        ResponseObject<?> result = new ResponseObject<>();

        UserOauthConfigResponse oauthConfig = userOauthConfigApi.oneData(Filter.eq(UserOauthConfigField.model, config));
        if (oauthConfig == null) {
            result.setCode(501);
            result.setMsg("没有配置文件");
            return result;
        }

        UserOauthTokenResponse userOauth = userOauthTokenApi.oneData(
                Filter.eq(UserOauthTokenField.unionId, unionId),
                Filter.eq(UserOauthTokenField.openId, openId),
                Filter.eq(UserOauthTokenField.userOauthConfig, oauthConfig.getId())
        );
        if (userId == null) {
            List<UserOauthTokenSimple> olds = userOauthTokenApi.listData(
                    Filter.eq(UserOauthTokenField.unionId, unionId),
                    Filter.ne(UserOauthTokenField.openId, openId),
                    Filter.isNotNull(UserOauthTokenField.user)
            );
            if (olds != null) {
                for (UserOauthTokenSimple old : olds) {
                    if (old.getUser() != null) {
                        userId = old.getUser();
                        break;
                    }
                }
                //查找没有用户的账号，设置对应的客户
            }
        }

        if (userOauth == null) {
            UserOauthTokenRequest req = new UserOauthTokenRequest();
            req.setUserOauthConfig(oauthConfig.getId());
            req.setOpenId(openId);
            req.setUnionId(unionId);
            req.setUser(userId);
            userOauthTokenApi.createData(req);
        }

        if (userId != null) {
            List<UserOauthTokenSimple> tokens = userOauthTokenApi.listData(
                    Filter.eq(UserOauthTokenField.unionId, unionId),
                    Filter.isNull(UserOauthTokenField.user)
            );
            if (tokens != null) {
                for (UserOauthTokenSimple token : tokens) {
                    UserOauthTokenRequest req = new UserOauthTokenRequest();
                    req.setId(token.getId());
                    req.setUser(userId);
                    userOauthTokenApi.updateData(req);
                }
            }
        }
        return result;
    }

    @Transactional
    public ResponseObject<?> unBind(String config, String openId) {
        ResponseObject<?> result=new ResponseObject<>();
        UserOauthConfigResponse oauthConfig = userOauthConfigApi.oneData(Filter.eq(UserOauthConfigField.model, config));
        if (oauthConfig == null) {
            result.setCode(501);
            result.setMsg("没有配置文件");
            return result;
        }
        UserOauthTokenResponse userOauth = userOauthTokenApi.oneData(
                Filter.eq(UserOauthTokenField.openId, openId),
                Filter.eq(UserOauthTokenField.userOauthConfig, oauthConfig.getId())
        );
        if (userOauth!=null){
            UserOauthTokenRequest req=new UserOauthTokenRequest();
            req.setId(userOauth.getId());
            userOauthTokenApi.delete(req);
        }

        return result;
    }
}
